<?php
namespace Admin\Controller;
use Common\Controller\CommonController;
class AdminController extends CommonController {
    public function _initialize(){
    	parent::_initialize();
    	if(!session('?uid')){
        	$this ->redirect('Login/index');
        }
         $data = $this -> getTreeData('level');
         $this -> assign('menu',$data);
         $auth=new \Think\Auth();
         $rule_name=MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
         $result=$auth->check($rule_name,$_SESSION['uid']);
        if(!$result){
        	$this->error('您没有权限访问');
        }
    }
    /**
     * $data  数据数组
     * $note  数据注释
     * $exclude 允许为空的字段
     * @param unknown $data
     * @param unknown $note
     * @param unknown $exclude
     */
    protected function checkField($data,$note,$exclude=array()){
    	foreach($data as $k=>$v){
    		$arr [] = array('key'=>$k,'value'=>$v,'note'=>$note[$k]);
    	}
    	foreach($arr as $kk=>$vv){
    		if($vv['value'] === null && !in_array($vv['key'],$exclude)){
    			$this -> error($vv['note'].'不能为空');
    		}
    	}
    	
    	return true;
    }
	/**
	 * 获取全部菜单
	 * @param  string $type tree获取树形结构 level获取层级结构
	 * @return array       	结构数据
	 */
	public function getTreeData($type='tree',$order=''){
		// 判断是否需要排序
		if(empty($order)){
			$data=M("Nav")->select();
		}else{
			$data=M("Nav")->order('order is null,'.$order)->select();
		}
		// 获取树形或者结构数据
		if($type=='tree'){
			$data=\Org\Nx\Data::tree($data,'name','id','pid');
		}elseif($type=="level"){
			$data=\Org\Nx\Data::channelLevel($data,0,'&nbsp;','id','parent_id');
			// 显示有权限的菜单
			$auth=new \Think\Auth();
			foreach ($data as $k => $v) {
				if ($auth->check($v['url'],$_SESSION['uid'])) {
					foreach ($v['_data'] as $m => $n) {
						if(!$auth->check($n['url'],$_SESSION['uid'])){
							unset($data[$k]['_data'][$m]);
						}
					}
				}else{
					// 删除无权限的菜单
					unset($data[$k]);
				}
			}
		}
		// p($data);die;
		return $data;
	}
	public function uploadAll(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     31457280000 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','swf');// 设置附件上传类型
		//$upload->rootPath  =     '/'; // 设置附件上传根目录
		$upload->savePath  =      '/'; // 设置附件上传目录
		// 上传文件
		$info   =  $upload->upload();
		$n = 0;
		//默认返回值
		//$data['msg'] = '没有上传数据';
		$data['status'] = '-1';
		$data['data'] =array(array('url'=>''));
		//判断上传信息
		if(!$info){
			$data['msg'] = $upload ->getError();
			return $data;
		}
		//组合返回信息
		foreach ($info as $k =>$v){
			// 上传成功
			$pic=$v['savepath'].$v['savename'];
			$url='/Uploads'.ltrim($pic,".");
			$data['data'][$n]['url'] =$url;
			$data['data'][$n]['name'] =$k;
			$n++;
		}
		$data['msg'] = '上传成功';
		$data['status'] = '1';
		return $data;
	}
		public function upload($file){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     999999999 ;// 设置附件上传大小
		$upload->exts      =      array('jpg', 'gif', 'png', 'jpeg','swf');
		$upload->savePath  =      '/'; // 设置附件上传目录
		// 上传文件
		$info   =  $upload->uploadOne($file);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());exit();
		}else{
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads'.ltrim($pic,".");
		}
		return $url;
	}
		/**
	 * 获取文章
	 * $PageArr = array(
	 *    'limit'=> $limit       ///int    查询条数
	 *    'where'=> $where       ///$where  查询条件
	 * )
	 */
	protected function getArticle($PageArr){
		$data = array(
            'page'=>'',
            'list'=>array(),
		);
		//定义分页条件
		$limit_page = '';
		$limit  = '';
		if(!empty($PageArr)){
			if(isset($PageArr['limit'])){
				$limit       =  (int)$PageArr['limit'];
				$count      =  M('Article')->where($PageArr['where'])->count();// 查询满足要求的总记录数
			    $Page       =  new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数
			    //手机展示2个
			    if($this ->isMobile()){
			    	$Page ->rollPage =2;
			    }
                $Page->parameter = $PageArr['where'];
                $data['page'] = $Page->show();
			}
			if(!isset($PageArr['limit']) && is_array($PageArr)){
				  $where   = $PageArr;
			}
			if(is_array($PageArr['where'])){
				   $where   = $PageArr['where'];
			}
		}
		//形成分页
		if($limit){
			$limit_page = $Page->firstRow.','.$limit;
		}
		$data['list'] = M('Article')->where($where)->limit($limit_page)->order('add_time desc')->select();
		return $data;
	}
}